package uk.ac.shef.ecomm.jdbc.mysql;

import java.sql.PreparedStatement;
import java.sql.SQLException;

import uk.ac.shef.ecomm.*;

public class ResearcherRegisteration extends Registeration
{
	private PreparedStatement sqlInsertLogin;
	private PreparedStatement sqlInsertRes;
	
	public ResearcherRegisteration()
	{
		try
		{
			connect();
			sqlInsertLogin = connection.prepareStatement("INSERT into " + UserTBL + " (email,password,type,last_access) VALUES (?,?,?,NOW())");
			sqlInsertRes = connection.prepareStatement("INSERT into " + ResearchersTBL + " (email,name) VALUES (?,?)");
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}
	public boolean register(Researcher r)
	{
		try
		{
			int result;
			
			sqlInsertLogin.setString(1, r.getEmail());
			sqlInsertLogin.setString(2, r.getPass());
			sqlInsertLogin.setString(3, r.getType());
			
			result = sqlInsertLogin.executeUpdate();
		
			if (result == 0)
			{
				connection.rollback();
				return false;
			}
			
			sqlInsertRes.setString(1, r.getEmail());
			sqlInsertRes.setString(2, r.getName());
			
			result = sqlInsertRes.executeUpdate();
			if (result == 0)
			{
				connection.rollback();
				return false;
			}
			
			connection.commit();
			return true;
			
		}
		catch (SQLException sqle)
		{
			sqle.printStackTrace();
			return false;
		}	
	}

	public void close()
	{
		try
		{
			sqlInsertLogin.close();
			sqlInsertRes.close();
			connection.close();
		}
		catch (SQLException ex)
		{
			ex.printStackTrace();
		}
	}
}
